/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 * \file     Eep_Irq.c                                                                            *
 * \brief    AUTOSAR 4.3.1 MCAL can Driver                                                             *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2024/11/04     <td>1.0.0                               *
 * </table>                                                                                             *
 *******************************************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif /* #ifdef __cplusplus */

#include "Eep.h"
#include "Mcal_MemLibc.h"

/* PRQA S 0314,0316,3673 EOF */

#if (EEP_USE_INTERRUPTS == STD_ON)

/********************************************************************************************************
 *                                 Private Macro definition                                             *
 *******************************************************************************************************/
#define EEP_IP_IRQ_HANDLER_FUNC(n) ISR(Eep_IrqHandlerController##n)\
{\
    Eep_IrqHandler(n);\
}

/********************************************************************************************************
 *                                    Private Type Declarations                                         *
 *******************************************************************************************************/

typedef struct
{
    Eep_HostContextType *hostCtx;
} Eep_IrqContextType;

/********************************************************************************************************
 *                                  Private Variable Definitions                                        *
 *******************************************************************************************************/
#define EEP_START_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Eep_MemMap.h"

static Eep_IrqContextType Eep_IrqContextTable[EEP_CFG_HOST_MAX_NUM];

#define EEP_STOP_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Eep_MemMap.h"

#define EEP_START_SEC_CODE
#include "Eep_MemMap.h"

static void Eep_EventListener(void *contex, uint32 event)
{
    const Eep_IrqContextType *irqCtx = (Eep_IrqContextType *)contex;
    if (NULL_PTR != irqCtx->hostCtx->config->eventNotification)
    {
        irqCtx->hostCtx->config->eventNotification(event);
    }
}

static void Eep_IrqHandler(uint8 controllerId)
{
    if (controllerId >= EEP_CFG_HOST_MAX_NUM)
    {
        /* Do nothing */
    }
    else if (NULL_PTR == Eep_IrqContextTable[controllerId].hostCtx)
    {
        /* Do nothing */
    }
    else
    {
        (void) Mram_HostIrqHandler(0u, (void*)&Eep_IrqContextTable[controllerId].hostCtx->mramHost);
    }
}

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/
void Eep_IrqEventSetup(uint8 controllerId, Eep_HostContextType *hostCtx)
{
    if (controllerId >= EEP_CFG_HOST_MAX_NUM)
    {
        /* Do nothing */
    }
    else
    {
        if (0 != Mram_HostEventSetup(&hostCtx->mramHost, (Mram_EventListenerType*)Eep_EventListener,
                            (void *)&Eep_IrqContextTable[controllerId]))
        {
            /* Do nothing */
        }
        else
        {
            Eep_IrqContextTable[controllerId].hostCtx = hostCtx;
        }
    }
}

void Eep_IrqEventDeinit(void)
{
    (void) Mcal_MemClr((void*)Eep_IrqContextTable, sizeof(Eep_IrqContextTable));
}

EEP_IP_IRQ_HANDLER_FUNC(0)
EEP_IP_IRQ_HANDLER_FUNC(1)
EEP_IP_IRQ_HANDLER_FUNC(2)
EEP_IP_IRQ_HANDLER_FUNC(3)
EEP_IP_IRQ_HANDLER_FUNC(4)

#define EEP_STOP_SEC_CODE
#include "Eep_MemMap.h"

#endif /* #if (EEP_USE_INTERRUPTS == STD_ON) */

#ifdef __cplusplus
}
#endif /* #ifdef __cplusplus */

/* End of file */
